import React, {Fragment, PropsWithChildren} from "react";
import { systemSettingPermission } from "@/components/app/commonSystemSetting";

import LoginUser from './LoginUser'
// 超级管理员的角色类型
const adminRoleType = "admin"

class Permission{
    loginUser = new LoginUser();

    // 验证是否为超级管理员
    isSuperAdmin = () => this.loginUser.get().roleType == adminRoleType

    can = (auth: string | {key: string}) => {
        if(this.isSuperAdmin()) return true;

        const {permissions} = this.loginUser.get();

        if(!auth || !permissions) {
            return false
        }

        if(typeof auth === 'string'){
            return permissions.includes(auth);
        }else {
            return permissions.includes(auth.key);
        }
    }

}

export const permission = new Permission()

/**
 * 权限组件
 */
export const AuthCan: React.FC<PropsWithChildren<{authKey: string}>> = ({authKey, children }) => React.createElement(Fragment, {children: permission.can(authKey) && children})


export const EnumAuthKey = {
    //-------------------------系统设置相关的路由------------------------------
    ...systemSettingPermission,
}